package com.mingdao.lhy.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.mingdao.lhy.biz.WishFillingBiz;
import com.mingdao.lhy.pojo.HighSchool;
import com.mingdao.lhy.pojo.ResponseVo;
import com.mingdao.lhy.pojo.page.ArtSpecialtySignUp;
import com.mingdao.lhy.pojo.page.ChangePassword;
import com.mingdao.lhy.service.HighSchoolService;
import com.mingdao.lhy.util.Validate;

@Controller
@RequestMapping("/highschool")
public class WishHighSchoolController {

	private static Logger log = LoggerFactory.getLogger(WishHighSchoolController.class);

	@Value("${gzshkssj}")
	private String gzshkssj;

	@Value("${gzshjssj}")
	private String gzshjssj;

	@Autowired
	private WishFillingBiz wishFillingBiz;

	@Autowired
	private HighSchoolService highSchoolService;

	@RequestMapping("")
	public String index() {
		return "loginHighSchool";
	}

	/**
	 * 高中学校登录验证页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	public String check(@ModelAttribute("highSchool") HighSchool highSchool, Model model) {

		HttpSession session = wishFillingBiz.getSession();

		log.debug("进入【高中学校登录验证页面】");
		if (null == highSchool || null == highSchool.getXxdm() || null == highSchool.getPassword()) {
			log.error("没有收到有效的请求数据");
			model.addAttribute("msg", "没有收到有效的请求数据");
			return "loginHighSchool";
		}
		/**
		 * 根据高中学校代码查询高中学校信息
		 */
		HighSchool highSchoolDb = new HighSchool();
		highSchoolDb = highSchoolService.getHighSchoolById(highSchool.getXxdm());
		if (null == highSchoolDb) {
			log.error("没有找到指定的高中学校");
			model.addAttribute("msg", "没有找到指定的高中学校");
			return "loginHighSchool";
		}

		/**
		 * 判断学校是否第一次登录（如果密码为空，或者和高中学校代码相同则是初次登录）
		 * 
		 * 如果是，跳转到修改密码界面，如果不是，则继续前进到艺体特色报名审核页面
		 */
		String dbPassword = highSchoolDb.getPassword().trim();
		if (null == dbPassword
				|| (dbPassword.equals(highSchoolDb.getXxdm()) && dbPassword.equals(highSchool.getPassword()))) {
			log.debug("高中学校初次登录 ，进入【密码修改页面】");
			model.addAttribute("highSchool", highSchoolDb);
			session.setAttribute("highSchool", highSchoolDb);
			// model.addAttribute("changePassword",new ChangePassword());
			return "highschool/changePasswdHighSchool";

		}

		// 验证是否找到对应的学校
		// 并且输入的密码是否和查找到的学校密码是否一致
		if (null != highSchoolDb.getPassword() && highSchoolDb.getPassword().equals(highSchool.getPassword())) {

			// 验证成功转到填报页面
			log.debug("验证【成功】");
			model.addAttribute("highSchool", highSchoolDb);
			if (null == session) {
				session = wishFillingBiz.getSession();
			}
			session.setAttribute("highSchool", highSchoolDb);

//			List<ArtSpecialtySignUp> students = highSchoolService
//					.getWishedStudentByGzxxdmArtSpecialty(highSchoolDb.getXxdm(), null);
//			model.addAttribute("students", students);

			// 转到艺体特色报名审核页面
			return "highschool/highschoolselect";
		} else {
			// 验证失败继续回到登录页面
			// Model model = new Model();
			log.debug("验证【失败】");

			model.addAttribute("msg", "验证失败，学校代码或密码不正确");
			return "loginHighSchool";
		}

	}

	/**
	 * 艺体特长
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("showHighSchoolInitYt")
	/*
	 * public List<ArtSpecialtySignUp> highSchoolWish2(@ModelAttribute("bmh") String
	 * bmh, Model model) {
	 */
	public String highSchoolWishYt(Model model) {

		/**
		 * 审核时间判断 6月17日08:00--6月20日23:59之间可用
		 */
		Date day = new Date();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		model.addAttribute("highSchool", highSchool);

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date now = df.parse(df.format(day)); // 当前时间
			Date startTime = df.parse(gzshkssj);
			Date endTime = df.parse(gzshjssj); // 截止时间
			if (now.before(endTime) && now.after(startTime)) {
				// 继续正常处理
				System.out.println("继续正常处理");

			} else {
				model.addAttribute("message", "不在审核时间范围内");
				return "highschool/highschoolselect";
			}
		} catch (Exception exception) {
			exception.printStackTrace();
			return "highschool/highschoolselect";
		}

		/* HttpSession session = wishFillingBiz.getSession(); */
		if (null == session || null == session.getAttribute("highSchool")) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			/* return "redirect:/loginCity"; */
		}

		/*
		 * List<ArtSpecialtySignUp> students =
		 * highSchoolService.getWishedStudentByGzxxdmArtSpecialty(highSchool.getXxdm(),
		 * null);
		 */
		return "highschool/wishFillingHighSchoolYt";
	}

	/**
	 * 艺体特长
	 * 
	 * @param bmh
	 * @param model
	 * @return
	 */
	@RequestMapping("showHighSchoolYt")
	@ResponseBody
	public List<ArtSpecialtySignUp> highSchoolWishYt(@ModelAttribute("bmh") String bmh, Model model) {
		/*
		 * public String highSchoolWish(@ModelAttribute("bmh") String bmh, Model model)
		 * {
		 */
		HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("highSchool")) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			/* return "redirect:/loginCity"; */
		}
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		model.addAttribute("highSchool", highSchool);

		List<ArtSpecialtySignUp> students = highSchoolService
				.getWishedStudentByGzxxdmArtSpecialtyYt(highSchool.getXxdm(), bmh);
		model.addAttribute("students", students);
		return students;
		// return "highschool/wishFillingHighSchoolTs";
	}

	/**
	 * 特色项目
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("showHighSchoolInitTs")
	/*
	 * public List<ArtSpecialtySignUp> highSchoolWish2(@ModelAttribute("bmh") String
	 * bmh, Model model) {
	 */
	public String highSchoolWishTs(Model model) {

		Date day = new Date();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		model.addAttribute("highSchool", highSchool);

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date now = df.parse(df.format(day)); // 当前时间
			Date startTime = df.parse(gzshkssj);
			Date endTime = df.parse(gzshjssj); // 截止时间
			if (now.before(endTime) && now.after(startTime)) {
				// 继续正常处理
				System.out.println("继续正常处理");

			} else {
				model.addAttribute("message", "不在审核时间范围内");
				return "highschool/highschoolselect";
			}
		} catch (Exception exception) {
			exception.printStackTrace();
			return "highschool/highschoolselect";
		}
		/* HighSchool highSchool = (HighSchool) session.getAttribute("highSchool"); */
		model.addAttribute("highSchool", highSchool);
		return "highschool/wishFillingHighSchoolTs";
	}

	/**
	 * 特色项目
	 * 
	 * @param bmh
	 * @param model
	 * @return
	 */
	@RequestMapping("showHighSchoolTs")
	@ResponseBody
	public List<ArtSpecialtySignUp> highSchoolWishTs(@ModelAttribute("bmh") String bmh, Model model) {
		/*
		 * public String highSchoolWish2(@ModelAttribute("bmh") String bmh, Model model)
		 * {
		 */

		HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("highSchool")) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			/* return "redirect:/loginCity"; */
		}
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		model.addAttribute("highSchool", highSchool);

		List<ArtSpecialtySignUp> students = highSchoolService
				.getWishedStudentByGzxxdmArtSpecialtyTs(highSchool.getXxdm(), bmh);
		model.addAttribute("students", students);
		return students;
		// return "highschool/wishFillingHighSchoolTs2";
	}

	@RequestMapping(value = "/doChangePasswd", method = RequestMethod.POST)
	public String changePwd(@ModelAttribute(value = "changePassword") ChangePassword changePassword,
			@SessionAttribute HighSchool highSchool, Model model) {

		HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("highSchool")) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			return "redirect:/loginHighSchool";
		}
		highSchool = (HighSchool) session.getAttribute("highSchool");
		/**
		 * 判断传入的密码
		 */
		if (null == model || null == model.asMap() || null == model.asMap().get("changePassword")) {
			log.error("未取到前台返回的密码值 ");
			model.addAttribute("msg", "未取到前台返回的密码值");
			// model.addAttribute("student", student);
			return "changePasswd";
		}
		// SignUpStudent student1 = (SignUpStudent) model.asMap().get("student");
		ChangePassword cp = (ChangePassword) model.asMap().get("changePassword");
		if (null == cp.getOldPassword()) {
			log.error("必须输入原来的密码值 ");
			model.addAttribute("msg", "必须输入原来的密码值");
			// model.addAttribute("student", student);
			return "check";
		}
		if (null == cp.getNewPasswordOne() || null == cp.getNewPasswordTwo()) {
			log.error("必须输入新的码值 ");
			model.addAttribute("msg", "必须输入新的码值");
			// model.addAttribute("student", student);
			return "changePasswd";
		}

		String oldPassword = cp.getOldPassword();
		String newPasswordOne = cp.getNewPasswordOne();
		String newPasswordTwo = cp.getNewPasswordTwo();

		/**
		 * 如果输入的旧密码等于高中学校本来的旧密码才可以更换密码
		 */
		if (!oldPassword.isEmpty() && !highSchool.getPassword().isEmpty()) {
			if (oldPassword.equals(highSchool.getPassword())) {
				/**
				 * 两次输入密码要一致才有效
				 */
				if (newPasswordOne.equals(newPasswordTwo)) {
					highSchool.setPassword(newPasswordOne);
				} else {
					log.error("两次密码不符，请重新核对 ");
					model.addAttribute("msg", "两次密码不符，请重新核对 ");
					model.addAttribute("highSchool", highSchool);
					return "changePasswd";
				}
			} else {
				log.error("原密码不符，请重新核对 ");
				model.addAttribute("msg", "原密码不符，请重新核对 ");
				model.addAttribute("highSchool", highSchool);
				return "/highschool/changePasswdHighSchool";
			}
		}

		/**
		 * 修改密码
		 */
		int result = highSchoolService.changePassword(highSchool);
		if (1 == result) {
			log.debug("密码修改成功");
		} else {
			log.warn("密码修改失败");
			model.addAttribute("highSchool", highSchool);
			return "highschool/changePasswdHighSchool";
		}
		/**
		 * 跳转到登录界面重新登录
		 */
		return "loginHighSchool";
	}

	/**
	 * 艺体特长
	 * 
	 * @param bmh
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doSelectStudentWishByBmhYt", method = RequestMethod.POST)
	public String selectStudentWishByBmhYt(@ModelAttribute("bmh") String bmh, Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		if (null == session || null == highSchool) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			vo.setCode("error");
			vo.setMessage("高中学校未登录，请重新登录");
			vo.setUrl("redirect:/loginHighSchool");
			// return vo;
			return "redirect:/loginHighSchool";
		}

		if (null == bmh || bmh.isEmpty()) {
			vo.setCode("stay");
			vo.setMessage("未收到查询条件报名号");
			vo.setUrl("highschool/wishFillingHighSchoolYt");
			/**
			 * 当报名号为空是查全体学生，不返回错误
			 */
			// return vo;
			// return "highschool/wishFillingHighSchoolTs";
		}
		List<ArtSpecialtySignUp> students = new ArrayList<ArtSpecialtySignUp>();

		students = highSchoolService.getWishedStudentByGzxxdmArtSpecialtyYt(highSchool.getXxdm(), bmh);

		model.addAttribute("students", students);
		model.addAttribute("highSchool", highSchool);

		// vo.setCode("ok");
		// vo.setMessage("查询完成");
		vo.setUrl("highschool/wishFillingHighSchoolYt");

		return "highschool/wishFillingHighSchoolYt";
	}

	/**
	 * 审核通过（艺体特长）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doExamineYt", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo examineYt(Model model, @RequestParam("bmh") String bmh) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		if (null == session || null == highSchool) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("高中学校未登录，请重新登录");
			vo.setUrl("redirect:/loginHighSchool");
			return vo;
		}
		/**
		 * 后台数据库保存审核状态（审核通过）
		 */
		int result = highSchoolService.updateHighSchoolExamineByBmhYt(bmh);
		if (result != 1) {
			log.error("审核状态更新失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("审核状态更新失败");
			vo.setUrl("highschool/wishFillingHighSchoolYt");
			return vo;
		}
		vo.setCode("ok");
		vo.setMessage("审核通过更新成功");
		vo.setUrl("highschool/wishFillingHighSchoolYt");
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	/**
	 * 审核未通过（艺体特长）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doCancelExamineYt", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo cancelExamineYt(Model model, @RequestParam("bmh") String bmh) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		if (null == session || null == highSchool) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("高中学校未登录，请重新登录");
			vo.setUrl("redirect:/loginHighSchool");
			return vo;
		}
		/**
		 * 后台数据库保存审核状态（审核未通过）
		 */
		int result = highSchoolService.updateHighSchoolCancelExamineByBmhYt(bmh);
		if (result != 1) {
			log.error("审核状态更新失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("审核状态更新失败");
			vo.setUrl("highschool/wishFillingHighSchoolYt");
			return vo;
		}
		vo.setCode("ok");
		vo.setMessage("审核未通过更新成功");
		vo.setUrl("highschool/wishFillingHighSchoolYt");
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	/**
	 * 提交成绩（特色项目）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doSaveScoreTs", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo saveScoreTs(@ModelAttribute("row") ArtSpecialtySignUp artSpecialtySignUp, Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		if (null == session || null == highSchool) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("高中学校未登录，请重新登录");
			vo.setUrl("redirect:/loginHighSchool");
			return vo;
		}
		// String row = (String) model.asMap().get("row");
		if (!Validate.isNumber(artSpecialtySignUp.getTskhcj())) {
			log.error("成绩验证失败");
			vo.setCode("error");
			vo.setMessage("成绩验证失败");
			vo.setUrl("highschool/wishFillingHighSchoolTs");
			return vo;
		}
		
		/**
		 * 后台数据库保存审核状态
		 */
		// String bmh=(String) model.asMap().get("bmh");
		int result = highSchoolService.updateHighSchoolScoreTs(artSpecialtySignUp);
		if (result != 1) {
			log.error("提交成绩失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("提交成绩失败");
			vo.setUrl("highschool/wishFillingHighSchoolTs");
			// return vo;
		} else {
			vo.setCode("ok");
			vo.setMessage("提交成绩成功");
			vo.setUrl("highschool/wishFillingHighSchoolTs");
		}
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	/**
	 * 提交成绩（艺体特长）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doSaveScoreYt", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo saveScoreYt(@ModelAttribute("row") ArtSpecialtySignUp artSpecialtySignUp, Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		if (null == session || null == highSchool) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("高中学校未登录，请重新登录");
			vo.setUrl("redirect:/loginHighSchool");
			return vo;
		}
		// String row = (String) model.asMap().get("row");
		if (!Validate.isNumber(artSpecialtySignUp.getYtkhcj())) {
			log.error("成绩验证失败");
			vo.setCode("error");
			vo.setMessage("成绩验证失败");
			vo.setUrl("highschool/wishFillingHighSchoolYt");
			return vo;
		}
		/**
		 * 后台数据库保存审核状态
		 */
		// String bmh=(String) model.asMap().get("bmh");
		int result = highSchoolService.updateHighSchoolScoreYt(artSpecialtySignUp);
		if (result != 1) {
			log.error("提交成绩失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("提交成绩失败");
			vo.setUrl("highschool/wishFillingHighSchoolYt");
			// return vo;
		} else {
			vo.setCode("ok");
			vo.setMessage("提交成绩成功");
			vo.setUrl("highschool/wishFillingHighSchoolYt");
		}
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	@RequestMapping(value = "forgetPwd", method = RequestMethod.POST)
	public String forgetPwd() {
		HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("student")) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			return "redirect:/";
		}
		return "forgetPasswd";
	}

	/**
	 * 艺体特长
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("showArtSpecialtyHigherYt")
	public String artSpecialtyYt(Model model) {
		HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("highSchool")) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			return "redirect:/loginHighSchool";
		}
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		model.addAttribute("highschool", highSchool);

		// List<SignUpStudent> students =
		// highSchoolService.getWishedStudentByXsdmWishedHigh(highSchool.getXxdm(),
		// null);
		// model.addAttribute("students", students);

		return "highschool/wishFillingHighSchoolYt";
	}

	/**
	 * 导出特色项目审核pdf
	 * 
	 * @author Changhai
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/exportpdfTs", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo exportPdfTs(@ModelAttribute("row") ArtSpecialtySignUp artSpecialtySignUp,
			HttpServletResponse response, Model model) {

		ResponseVo vo = new ResponseVo();

		// 指定解析器
		System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
				"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");

		PdfStamper psTsxm = null;
		// PdfStamper psYttc = null;
		PdfReader readerTsxm = null;
		// PdfReader readerYttc = null;
		ByteArrayOutputStream bosTsxm = null;
		// ByteArrayOutputStream bosYttc = null;
		try {
			String basePath = ResourceUtils.getURL("classpath:static/highschool/").getPath();
			String templateTsxm = basePath + "凉山州年普通高中特色项目班报名表.pdf";
			// String templateYttc = basePath + "凉山州2018年普通高中艺体特长生报名表.pdf";
			String newPathTsxm = basePath + UUID.randomUUID() + ".pdf";
			// String newPathYttc = basePath + UUID.randomUUID() + ".pdf";

			// response.setContentType("application/pdf");
			// response.setHeader("Content-Disposition",
			// "attachment;fileName=" + URLEncoder.encode(templateFileName, "UTF-8"));

			bosTsxm = new ByteArrayOutputStream();
			// bosYttc = new ByteArrayOutputStream();

			// os = response.getOutputStream();
			// 2 读入pdf表单
			readerTsxm = new PdfReader(templateTsxm);
			// readerYttc = new PdfReader(templateYttc);

			// 3 根据表单生成一个新的pdf
			psTsxm = new PdfStamper(readerTsxm, bosTsxm);
			// psYttc = new PdfStamper(readerYttc, bosYttc);

			// 4 获取pdf表单
			AcroFields formTsxm = psTsxm.getAcroFields();
			// AcroFields formYttc = psYttc.getAcroFields();
			// 5给表单添加中文字体 这里采用系统字体。不设置的话，中文可能无法显示
			BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			formTsxm.addSubstitutionFont(bf);
			// formYttc.addSubstitutionFont(bf);
			// 6查询数据================================================
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("xm", artSpecialtySignUp.getXm());
			data.put("xb", artSpecialtySignUp.getXb());
			data.put("mz", artSpecialtySignUp.getMz());
			data.put("zzmm", artSpecialtySignUp.getZzmm());
			data.put("csrq", artSpecialtySignUp.getCsrq());
			data.put("sfzh", artSpecialtySignUp.getSfzh());
			data.put("txdz", artSpecialtySignUp.getTxdz());
			data.put("byzxmc", artSpecialtySignUp.getByzxdm());
			data.put("byzxdm", artSpecialtySignUp.getByzxdm());
			data.put("bmh", artSpecialtySignUp.getBmh());
			data.put("xjh", artSpecialtySignUp.getXjh());
			data.put("tszc", artSpecialtySignUp.getTsxmcl());
			// data.put("yttc", artSpecialtySignUp.getYttccl());
			data.put("gzxxmc", artSpecialtySignUp.getGzxxmcts());
			data.put("gzxxdm", artSpecialtySignUp.getGzxxdmts());
			data.put("tsxmdm", artSpecialtySignUp.getTszydm());
			data.put("tszymc", artSpecialtySignUp.getTszymc());
			data.put("tsxmcj", artSpecialtySignUp.getTskhcj());
			// data.put("yttcdm", artSpecialtySignUp.getYtzydm());
			// data.put("yttcmc", artSpecialtySignUp.getYtzymc());
			// data.put("yttccj", artSpecialtySignUp.getYtkhcj());
			// 7遍历data 给pdf表单表格赋值
			for (String key : data.keySet()) {
				formTsxm.setField(key, data.get(key).toString());
				// formYttc.setField(key, data.get(key).toString());
			}
			psTsxm.setFormFlattening(true);
			psTsxm.close();
			// psYttc.setFormFlattening(true);
			// psYttc.close();
			// -----------------------------pdf 添加图片----------------------------------
			/*
			 * // 通过域名获取所在页和坐标，左下角为起点 System.out.println("pdf 添加图片"); String imgpath =
			 * "e:/美女.png"; int pageNo = form.getFieldPositions("img").get(0).page;
			 * Rectangle signRect = form.getFieldPositions("img").get(0).position; float x =
			 * signRect.getLeft(); float y = signRect.getBottom(); // 读图片 Image image =
			 * Image.getInstance(imgpath); // 获取操作的页面 PdfContentByte under =
			 * ps.getOverContent(pageNo); // 根据域的大小缩放图片
			 * image.scaleToFit(signRect.getWidth(), signRect.getHeight()); // 添加图片
			 * image.setAbsolutePosition(x, y); under.addImage(image);
			 */

			FileOutputStream outTsxm = new FileOutputStream(newPathTsxm);// 输出流
			// FileOutputStream outYttc = new FileOutputStream(newPathYttc);// 输出流
			Document docTsxm = new Document();
			PdfCopy copyTsxm = new PdfCopy(docTsxm, outTsxm);
			docTsxm.open();
			PdfImportedPage importPageTsxm = copyTsxm.getImportedPage(new PdfReader(bosTsxm.toByteArray()), 1);
			copyTsxm.addPage(importPageTsxm);
			docTsxm.close();

//			Document docYttc = new Document();
//			PdfCopy copyYttc = new PdfCopy(docYttc, outYttc);
//			docYttc.open();
//			PdfImportedPage importPageYttc = copyYttc.getImportedPage(new PdfReader(bosYttc.toByteArray()), 1);
//			copyYttc.addPage(importPageYttc);
//			docYttc.close();

			// -------------------------------------------------------------
			File tempFileTs = new File(newPathTsxm);
			String fileName = tempFileTs.getName();

			// File tempFile2 = new File(newPathYttc);
			// fileName = fileName + "," + tempFile2.getName();

			System.out.println("===============PDF导出成功=============");
			vo.setCode("ok");
			vo.setMessage(fileName);
			vo.setUrl("highschool/wishFillingHighSchoolTs");
			return vo;
		} catch (Exception e) {
			System.out.println("===============PDF导出失败=============");
			e.printStackTrace();
		} finally {
			try {
				psTsxm.close();
				readerTsxm.close();
				bosTsxm.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		vo.setCode("error");
		vo.setMessage("PDF导出失败");
		vo.setUrl("highschool/wishFillingHighSchoolTs");
		return vo;
	}

	/**
	 * 根据报名号查学校报名信息（特色项目）
	 * 
	 * @param bmh
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doSelectStudentWishByBmhTs", method = RequestMethod.POST)
	public String selectStudentWishByBmhTs(@ModelAttribute("bmh") String bmh, Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		if (null == session || null == highSchool) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			vo.setCode("error");
			vo.setMessage("高中学校未登录，请重新登录");
			vo.setUrl("redirect:/loginHighSchool");
			// return vo;
			return "redirect:/loginHighSchool";
		}

		if (null == bmh || bmh.isEmpty()) {
			vo.setCode("stay");
			vo.setMessage("未收到查询条件报名号");
			vo.setUrl("highschool/wishFillingHighSchoolTs");
			/**
			 * 当报名号为空是查全体学生，不返回错误
			 */
			// return vo;
			// return "highschool/wishFillingHighSchoolTs";
		}
		List<ArtSpecialtySignUp> students = new ArrayList<ArtSpecialtySignUp>();

		students = highSchoolService.getWishedStudentByGzxxdmArtSpecialtyTs(highSchool.getXxdm(), bmh);

		model.addAttribute("students", students);
		model.addAttribute("highSchool", highSchool);

		// vo.setCode("ok");
		// vo.setMessage("查询完成");
		vo.setUrl("highschool/wishFillingHighSchoolTs");

		return "highschool/wishFillingHighSchoolTs";
	}

	/**
	 * 审核通过（特色项目）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doExamineTs", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo examineTs(Model model, @RequestParam("bmh") String bmh) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		if (null == session || null == highSchool) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("高中学校未登录，请重新登录");
			vo.setUrl("redirect:/loginHighSchool");
			return vo;
		}
		/**
		 * 后台数据库保存审核状态（审核通过）
		 */
		int result = highSchoolService.updateHighSchoolExamineByBmhTs(bmh);
		if (result != 1) {
			log.error("审核状态更新失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("审核状态更新失败");
			vo.setUrl("highschool/wishFillingHighSchoolTs");
			return vo;
		}
		vo.setCode("ok");
		vo.setMessage("审核通过更新成功");
		vo.setUrl("highschool/wishFillingHighSchoolTs");
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	/**
	 * 审核未通过（特色项目）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doCancelExamineTs", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo cancelExamineTs(Model model, @RequestParam("bmh") String bmh) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		if (null == session || null == highSchool) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("高中学校未登录，请重新登录");
			vo.setUrl("redirect:/loginHighSchool");
			return vo;
		}
		/**
		 * 后台数据库保存审核状态（审核未通过）
		 */
		int result = highSchoolService.updateHighSchoolCancelExamineByBmhTs(bmh);
		if (result != 1) {
			log.error("审核状态更新失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("审核状态更新失败");
			vo.setUrl("highschool/wishFillingHighSchoolTs");
			return vo;
		}
		vo.setCode("ok");
		vo.setMessage("审核未通过更新成功");
		vo.setUrl("highschool/wishFillingHighSchoolTs");
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	/**
	 * 特色项目
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("showArtSpecialtyHigherTs")
	public String artSpecialtyTs(Model model) {
		HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("highSchool")) {
			session = null;
			log.error("高中学校未登录，请重新登录");
			return "redirect:/loginHighSchool";
		}
		HighSchool highSchool = (HighSchool) session.getAttribute("highSchool");
		model.addAttribute("highschool", highSchool);

		// List<SignUpStudent> students =
		// highSchoolService.getWishedStudentByXsdmWishedHigh(highSchool.getXxdm(),
		// null);
		// model.addAttribute("students", students);

		return "highschool/wishFillingHighSchoolTs";
	}

	/**
	 * 导出艺体特长审核pdf
	 * 
	 * @author Changhai
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/exportpdfYt", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo exportPdfYt(@ModelAttribute("row") ArtSpecialtySignUp artSpecialtySignUp,
			HttpServletResponse response, Model model) {

		ResponseVo vo = new ResponseVo();

		// 指定解析器
		System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
				"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");

		// PdfStamper psTsxm = null;
		PdfStamper psYttc = null;
		// PdfReader readerTsxm = null;
		PdfReader readerYttc = null;
		// ByteArrayOutputStream bosTsxm = null;
		ByteArrayOutputStream bosYttc = null;
		try {
			String basePath = ResourceUtils.getURL("classpath:static/highschool/").getPath();
			// String templateTsxm = basePath + "凉山州2018年普通高中特色项目班报名表.pdf";
			String templateYttc = basePath + "凉山州年普通高中艺体特长生报名表.pdf";
			// String newPathTsxm = basePath + UUID.randomUUID() + ".pdf";
			String newPathYttc = basePath + UUID.randomUUID() + ".pdf";

			// response.setContentType("application/pdf");
			// response.setHeader("Content-Disposition",
			// "attachment;fileName=" + URLEncoder.encode(templateFileName, "UTF-8"));

			// bosTsxm = new ByteArrayOutputStream();
			bosYttc = new ByteArrayOutputStream();

			// os = response.getOutputStream();
			// 2 读入pdf表单
			// readerTsxm = new PdfReader(templateTsxm);
			readerYttc = new PdfReader(templateYttc);

			// 3 根据表单生成一个新的pdf
			// psTsxm = new PdfStamper(readerTsxm, bosTsxm);
			psYttc = new PdfStamper(readerYttc, bosYttc);

			// 4 获取pdf表单
			// AcroFields formTsxm = psTsxm.getAcroFields();
			AcroFields formYttc = psYttc.getAcroFields();
			// 5给表单添加中文字体 这里采用系统字体。不设置的话，中文可能无法显示
			BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			// formTsxm.addSubstitutionFont(bf);
			formYttc.addSubstitutionFont(bf);
			// 6查询数据================================================
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("xm", artSpecialtySignUp.getXm());
			data.put("xb", artSpecialtySignUp.getXb());
			data.put("mz", artSpecialtySignUp.getMz());
			data.put("zzmm", artSpecialtySignUp.getZzmm());
			data.put("csrq", artSpecialtySignUp.getCsrq());
			data.put("sfzh", artSpecialtySignUp.getSfzh());
			data.put("txdz", artSpecialtySignUp.getTxdz());
			data.put("byzxmc", artSpecialtySignUp.getByzxdm());
			data.put("byzxdm", artSpecialtySignUp.getByzxdm());
			data.put("bmh", artSpecialtySignUp.getBmh());
			data.put("xjh", artSpecialtySignUp.getXjh());
			data.put("tszc", artSpecialtySignUp.getTsxmcl());
			data.put("yttc", artSpecialtySignUp.getYttccl());
			data.put("gzxxmc", artSpecialtySignUp.getGzxxmc());
			data.put("gzxxdm", artSpecialtySignUp.getGzxxdm());
			data.put("tsxmdm", artSpecialtySignUp.getTszydm());
			data.put("tsxmcj", artSpecialtySignUp.getTskhcj());
			data.put("yttcdm", artSpecialtySignUp.getYtzydm());
			data.put("yttcmc", artSpecialtySignUp.getYtzymc());
			data.put("yttccj", artSpecialtySignUp.getYtkhcj());
			// 7遍历data 给pdf表单表格赋值
			for (String key : data.keySet()) {
				// formTsxm.setField(key, data.get(key).toString());
				formYttc.setField(key, data.get(key).toString());
			}
			// psTsxm.setFormFlattening(true);
			// psTsxm.close();
			psYttc.setFormFlattening(true);
			psYttc.close();
			// -----------------------------pdf 添加图片----------------------------------
			/*
			 * // 通过域名获取所在页和坐标，左下角为起点 System.out.println("pdf 添加图片"); String imgpath =
			 * "e:/美女.png"; int pageNo = form.getFieldPositions("img").get(0).page;
			 * Rectangle signRect = form.getFieldPositions("img").get(0).position; float x =
			 * signRect.getLeft(); float y = signRect.getBottom(); // 读图片 Image image =
			 * Image.getInstance(imgpath); // 获取操作的页面 PdfContentByte under =
			 * ps.getOverContent(pageNo); // 根据域的大小缩放图片
			 * image.scaleToFit(signRect.getWidth(), signRect.getHeight()); // 添加图片
			 * image.setAbsolutePosition(x, y); under.addImage(image);
			 */

			// FileOutputStream outTsxm = new FileOutputStream(newPathTsxm);// 输出流
			FileOutputStream outYttc = new FileOutputStream(newPathYttc);// 输出流
			// Document docTsxm = new Document();
			// PdfCopy copyTsxm = new PdfCopy(docTsxm, outTsxm);
			// docTsxm.open();
			// PdfImportedPage importPageTsxm = copyTsxm.getImportedPage(new
			// PdfReader(bosTsxm.toByteArray()), 1);
			// copyTsxm.addPage(importPageTsxm);
			// docTsxm.close();

			Document docYttc = new Document();
			PdfCopy copyYttc = new PdfCopy(docYttc, outYttc);
			docYttc.open();
			PdfImportedPage importPageYttc = copyYttc.getImportedPage(new PdfReader(bosYttc.toByteArray()), 1);
			copyYttc.addPage(importPageYttc);
			docYttc.close();

			// -------------------------------------------------------------
			// File tempFile1 = new File(newPathTsxm);
			// String fileName = tempFile1.getName();

			File tempFileYt = new File(newPathYttc);
			// fileName = fileName + "," + tempFile2.getName();
			String fileName = tempFileYt.getName();

			System.out.println("===============PDF导出成功=============");
			vo.setCode("ok");
			vo.setMessage(fileName);
			vo.setUrl("highschool/wishFillingHighSchoolYt");
			return vo;
		} catch (Exception e) {
			System.out.println("===============PDF导出失败=============");
			e.printStackTrace();
		} finally {
			try {
				// psTsxm.close();
				// readerTsxm.close();
				// bosTsxm.close();
				psYttc.close();
				readerYttc.close();
				bosYttc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		vo.setCode("error");
		vo.setMessage("PDF导出失败");
		vo.setUrl("highschool/wishFillingHighSchoolYt");
		return vo;
	}

}
